package simple; /**
 * @projectName study-note
 * @package PACKAGE_NAME
 * @className PACKAGE_NAME.RemoveDuplicates
 */

/**
 * RemoveDuplicates
 * @description 删除有序数组中的重复项
 * @author answer_wx
 * @date 2022/6/30 17:29
 * @version 1.0
 */
public class RemoveDuplicates {
    public static int removeDuplicates(int[] nums) {
        int length = nums.length;
        if (length <= 1) {
            return length;
        }
        int i = 0, j = 1;
        while (j < length) {
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
            j++;
        }
        return i + 1;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(removeDuplicates(nums));
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + ",");
        }
    }
}